<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>测试reactive</h2>
      <p id="name"></p>
      <p id="age"></p>
      <p id="sex"></p>
      
      <h2>测试ref</h2>
      <p id="count"></p>
      <p id="user"></p>
    </div>

    <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.2.47/vue.global.js"></script>
    <script>
      const { reactive, effect, ref } = Vue
      // effect: 用来注册副作用函数的函数
      // 定义reactive对象

      // const state = reactive({
      //   name: 'tom',
      //   age: 12
      // })
      // effect(function effect1() {
      //   console.log('name effect')
      //   document.getElementById('name').innerHTML = state.name
      // })
      // effect(function effect2() {
      //   console.log('age effect')
      //   document.getElementById('age').innerHTML = state.age
      // })
      // effect(function effect3() {
      //   console.log('sex effect')
      //   document.getElementById('sex').innerHTML = state.sex
      // })

      // setTimeout(() => {
      //   // 更新已有属性
      //   // state.name = 'Jack'
      //   // state.age = 13
      //   // 添加新属性
      //   // state.sex = '男'
      //   // 删除属性
      //   delete state.name
      // }, 2000);
    </script>
    <script>
      const count = ref(1)
      const user = ref({
        name: 'Jack',
        age: 12
      })

      effect(() => {
        console.log('count effect')
       document.getElementById('count').innerHTML = count.value
      })

      effect(() => {
        console.log('user effect')
        document.getElementById('user').innerHTML = user.value.name + '---' + user.value.age
      })

      setTimeout(() => {
        // count.value = 2

        // user.value = {
        //   name: 'Tom',
        //   age: 14
        // }

        // user.value.name = 'BOB'
      }, 2000);

      reactive({
        a: 1,
        b: {
          b: 2,
          d: 3
        }
      })
    </script>
  </body>
</html>
